from tkinter import *
class Tlist(Frame):
    def __init__(self, master,ulist,filename) -> None:
        super().__init__(master=master)
        self.length=len(ulist)#使用这个，代替len(ulist)
        self.ulist=ulist            
        self.username=None
        self.password=None
        self.sex=None
        self.money=None
        self.Llist=[None]*self.length
        self.Elist=[None]*self.length
        self.Vlist=[None]*self.length
        self.filename=filename
        master.title=self.username
        master.title=self.password
        master.title=self.sex
        master.title=self.money
        for i in range(self.length):
            self.Vlist[i]=StringVar()
        self.createwidget()
    def createwidget(self):
        for i,item in enumerate(self.ulist):
            self.Llist[i]=Label(self,text=item,bg="gold",width=15,font="宋体,20")
            self.Llist[i].grid(row=i+1,column=1)
            self.Elist[i]=Entry(self,width=20,textvariable=self.Vlist[i])
            self.Elist[i].grid(row=i+1,column=2)
        self.btreg=Button(self,text="注册",bg="gold",width=20,font="宋体,20")
        self.btreg.grid(row=self.length+1,column=1)
        self.btreg.bind("<Button-1>",self.clickreg)
    def clickreg(self,event):
        s=dict()
        for i,item in enumerate(self.ulist):
            s[item]=self.Vlist[i].get()
            print(s)
        with open(self.filename,'a+',encoding="utf8") as f:
            f.write(str(s)+"\n")



root=Tk()
root.geometry("400x500")
ulist=("用户名","密码","性别","账户金额")
app=Tlist(root,ulist,r"E:\Python文件\随堂测试\Python大作业\reg.txt")
app.grid(row=1,column=1)
root.mainloop()

